package net.tinyos.dingotos;

import net.tinyos.util.*;
import net.tinyos.message.*;
import java.util.Date;
import java.io.*; 

/** DingoTos is ...
 * <p>
 * @version 1.2
 * @author Sarah Mount <s.mount@wlv.ac.uk>
 **/
public class DingoTos implements MessageListener {

	private MoteIF mote; 
	private int msgCounter; // # of messages recieved from /all/ motes.

	private static final int BUFFER_SIZE = 1;

	public DingoTos() {
		mote = new MoteIF(PrintStreamMessenger.err);
		mote.registerListener(new SampleMsg(), this);
		msgCounter = 0;
	}

	/**
	 * The main method of the program
	 * @param args the arguments for the program
	 **/
	public static void main(String[] args) {
		new DingoTos();
		System.err.println("Waiting for messages...");
	}
	
	/**
	 * This method is invoked whenever a message is receieved by the
	 * serial forwarder.
	 *
	 * @param dest_addr the address the message was going to
	 * @param msg the message that was sent
	 * @throws RuntimeException if the message type is bad
	 **/
	public void messageReceived(int dest_addr, Message msg) {
		if (msg instanceof SampleMsg) {
			SampleMsg newMsg = (SampleMsg)msg;
			this.msgCounter++;
			System.out.print("Received message: " + this.msgCounter);
			System.out.println(" from mote " + newMsg.get_sourceMoteID());
			switch(newMsg.get_channel()) {
			case 1: // Light
				lightReceived(newMsg);
				break;
			case 2: // Accel
				accelReceived(newMsg);
				break;
			case 4: // Button
				buttonReceived(newMsg);
				break;
			case 8: // Temp
				tempReceived(newMsg);
				break;
			case 16: // Voltage
				voltageReceived(newMsg);
				break;
			case 32: // Sound level
				soundLevelReceived(newMsg);
				break;
			default:
				throw new RuntimeException("Got unknown channel number " + 
											newMsg.get_channel() + 
											msg);
			}

		}
		else {
			throw new RuntimeException("Got unknown message type " + msg);
		}
	}
	
	/**
	 * When msg is recieved with Light channel
	 **/
	private void lightReceived(SampleMsg msg) {
		int sample = msg.get_sample();
		System.out.println("Light sample:\t" + sample);
	}
	
	/**
	 * When msg is recieved with Accel channel
	 **/
	private void accelReceived(SampleMsg msg) {
		int accelX, accelY;
		accelX = msg.get_sample();
		accelY = msg.get_accelY();
		System.out.println("Accel sample (X,Y):\t" + "(" + accelX + ", " + accelY + ")");
	}
	
	/**
	 * When msg is recieved with Button channel
	 **/
	private void buttonReceived(SampleMsg msg) {
		if (msg.get_sample() == 0)
			System.out.println("Button down...");
		else System.out.println("Button up...");
	}

	/**
	 * When msg is recieved with Light channel
	 **/
	private void tempReceived(SampleMsg msg) {
		int sample = msg.get_sample();
		System.out.println("Temp sample:\t" + sample);
	}
	
	/**
	 * When msg is recieved with Voltage channel
	 **/
	private void voltageReceived(SampleMsg msg) {
		int sample = msg.get_sample();
		System.out.println("Voltage sample:\t" + sample);
	}
	
	/**
	 * When msg is recieved with Sound channel
	 **/
	private void soundLevelReceived(SampleMsg msg) {
		int sample = msg.get_sample();
     	System.out.println("Sound level:\t" + sample);
	}

}
